﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using System.Configuration;
using DotNet.Common;

namespace DotNet.Data.Factory
{
    /// <summary>
    /// 数据工厂类，根据设置，确定使用那种数据库
    /// </summary>
    public class DBFactory
    {
        public static IDBHelp Create()
        {
            if (!CacheUtil.IsExist("DotNet_dbInstance"))
            {
                string path = "DotNet.Data";
                //数据库实例
                string instance = "";
                string dbType = DotConfig.GetAppSettingsValue("DBType");
                string accInstance = "Factory.AccHelp";
                switch (dbType.Trim())
                {
                    case "1":
                        instance = accInstance;
                        break;
                    case "2":
                        instance = "Factory.SqlHelp";
                        break;
                    default:
                        instance = accInstance;
                        break;
                }
                string className = path + "." + instance;                
                object dbObj = Assembly.Load(path).CreateInstance(className);
                CacheUtil.Insert("DotNet_dbInstance", dbObj, 10 * 24 * 60 * 60);
            }
            return (IDBHelp)CacheUtil.Read("DotNet_dbInstance");
        }

    }
}
